using NUnit.Framework;
using Tycho.Parser.Peg;

namespace Tycho.Parser.Tests.Peg {
    [TestFixture]
    public class ProductionParserSpecification {
        [Test]
        public void ShouldReturnTermParsedIfSuccessful () {
            var production = new NonTerminal("alphabet", new SequenceRule (new KeywordRule ("a"), new KeywordRule (","), new KeywordRule ("b")));
            var parser = new ProductionParser (production);

            string source = "a, b";
            string filename = "filename";
            ITerm term = parser.Parse (source, filename);

            Assert.That (term, Is.Not.Null);
        }

        [Test]
        public void ShouldReturnTermParsedIfSuccessfulAndWhitespaceOnEnd () {
            var production = new NonTerminal("alphabet", new SequenceRule (new KeywordRule ("a"), new KeywordRule (","), new KeywordRule ("b")));
            var parser = new ProductionParser (production);

            string source = "a, b ";
            string filename = "filename";
            ITerm term = parser.Parse (source, filename);

            Assert.That (term, Is.Not.Null);
        }

        [Test]
        public void ShouldThrowExceptionIfNotAllSourceIsParsed () {
            var production = new NonTerminal("alphabet", new SequenceRule (new KeywordRule ("a"), new KeywordRule (","), new KeywordRule ("b")));
            var parser = new ProductionParser (production);

            string source = "a, b, c";
            string filename = "filename";
            Assert.That(() => { parser.Parse (source, filename); }, Throws.TypeOf(typeof (SyntaxException)));
        }

        [Test]
        public void ToCharArrayShouldReturnNewArrayForEachInvocation () {
            string s = "one";

            Assert.That (s.ToCharArray(), Is.Not.SameAs (s.ToCharArray()));
        }
    }
}